package com.wanqing.assistant.tools;

import com.wanqing.dto.*;
import com.wanqing.entity.AddressBook;
import com.wanqing.entity.Book;
import com.wanqing.entity.Category;
import com.wanqing.entity.ShoppingCart;
import com.wanqing.mapper.BookMapper;
import com.wanqing.mapper.OrderMapper;
import com.wanqing.result.PageResult;
import com.wanqing.service.*;
import com.wanqing.vo.OrderSubmitVO;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Component
@Slf4j
public class UserTools {

    @Autowired
    private BookService bookService;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private VoucherService voucherService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Tool(name = "图书/书单分类查询", value = "查询图书或者书单分类，并返回所有分类名称")
    public List<Category> queryCategory(@P(value = "图书分类类型 类型: 1图书分类 2书单分类", required = false) Integer type) {
        log.info("图书/书单分类查询被调用");
        return categoryService.list(type);
    }

    @Tool(name = "图书资讯", value = "查询图书,并介绍该图书")
    public Book queryBook(@P(value = "图书信息") BookDTO bookDTO) {
        log.info("图书资讯被调用");
        return bookService.getBook(bookDTO);
    }

    @Tool(name = "图书推荐", value = "先不进行分类id传参，查询近一个月销量前十图书进行推荐。还可提供图书分类，根据图书分类和图书风格进行推荐，给出建议和介绍")
    public List<Book> bookRecommend(
            @P(value = "开始时间yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @P(value = "结束时间yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end,
            @P(value = "图书分类", required = false) Long categoryId) {
        log.info("图书推荐被调用");
        if (categoryId == null) {
            LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
            //查询前十的图书和销量
            List<GoodsSalesDTO> goodsSalesDTOList = orderMapper.getSalesTop10(beginTime, endTime);
            //获取名称和数量
            List<String> salesNameList = goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).toList();
            return salesNameList.stream().map(name -> bookMapper.getByName(name)).toList();
        }
        return bookService.getByCategoryId(categoryId);
    }

    @Tool(name = "智能下单", value = "智能下单，智能提示用户操作进行下单。" +
            "查询用户地址，如果没地址提示用户去新增地址，如果有地址，列出来让用户选择，地址都不符合,提示用户去新建地址。" +
            "查询该用户是否有相关优惠券，如果有让用户选择一个优惠券进行下单。" +
            "下单完成返回订单信息，提示用户尽快完成支付")
    public OrderSubmitVO smartOrder(@P(value = "订单提交项 userId为当前对话用户Id，remark询问用户需要备注吗,配送状态为1 立即送出 预计送达时间estimatedDeliveryTime格式写正确(yyyy-MM-dd HH:mm:ss)") OrdersSubmitDTO ordersSubmitDTO) {
        log.info("智能下单被调用");
        return orderService.submitOrder(ordersSubmitDTO);
    }
    @Tool(name = "添加商品到购物车", value = "添加商品到购物车，并返回相关信息")
    public void addShoppingCart(@P(value = "购物车项") ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：{}", shoppingCartDTO);
        // 调用服务层方法添加商品到购物车
        shoppingCartService.addShoppingCart(shoppingCartDTO);
    }

    @Tool(name = "删除购物车商品", value = "删除购物车商品，并返回相关信息")
    public void deleteShoppingCart(@P(value = "购物车项") ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车商品：{}", shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
    }

    @Tool(name = "查询购物车商品", value = "查询购物车商品，返回购物车商品信息")
    public List<ShoppingCart> queryShoppingCart(@P(value = "当前用户Id") Long userId) {
        log.info("查询购物车被调用");
        return shoppingCartService.showShoppingCart(userId);
    }

    @Tool(name = "清空购物车", value = "清空购物车，并返回相关信息")
    public void cleanShoppingCart(@P(value = "当前用户Id") Long userId) {
        log.info("清空购物车被调用");
        shoppingCartService.cleanShoppingCart(userId);
    }

    @Tool(name = "查询用户地址簿", value = "查询用户地址簿，返回地址簿信息")
    public List<AddressBook> queryAddressBook(@P(value = "当前用户Id") Long userId) {
        log.info("查询地址簿被调用");
        //查看当前线程
        log.info("当前线程:{}", Thread.currentThread());
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(userId);
        return addressBookService.list(addressBook);
    }

    @Tool(name = "查询用户优惠券", value = "查询用户优惠券，返回优惠券信息")
    public List queryUserCoupon(@P(value = "当前用户Id") Long userId) {
        log.info("查询用户优惠券被调用");
        UserVoucherPageQueryDTO userVoucherPageQueryDTO = new UserVoucherPageQueryDTO();
        userVoucherPageQueryDTO.setUserId(userId);
        userVoucherPageQueryDTO.setStatus((byte) 0);
        userVoucherPageQueryDTO.setPage(1);
        userVoucherPageQueryDTO.setPageSize(50);
        PageResult list = voucherService.listUserVouchers(userVoucherPageQueryDTO);
        return list.getRecords();
    }
}
